package com.animee.day15.slice;
import com.animee.day15.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Picker;
import ohos.agp.components.Text;
//        picker.setMinValue(0);  //设置最小值
//        picker.setMaxValue(6);  //设置最大值
//设置显示数据
//        String[]arr = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
//        picker.setDisplayedData(arr);
/** 无限循环播放的页面*/
public class PickerAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    Picker picker,picker2;
    Text text;
    Button button;
    String []provinces = {"黑龙江","吉林","辽宁","甘肃"};
    String [][]citys = {{"哈尔滨","黑河","漠河"},{"长春","延吉","吉林"}
            ,{"沈阳","大连","朝阳","鞍山","阜新"}
            ,{"兰州","天水","嘉峪关","酒泉市"}};

    int selectProIndex = 0;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_picker);
        initView();
    }

    private void initView() {
        picker = findComponentById(ResourceTable.Id_pk_picker1);
        picker2 = findComponentById(ResourceTable.Id_pk_picker2);
        text = findComponentById(ResourceTable.Id_pk_tv);
        button = findComponentById(ResourceTable.Id_pk_btn);
        button.setClickedListener(this);

        picker.setMinValue(0);
        picker.setMaxValue(provinces.length-1);
        picker.setFormatter(new Picker.Formatter() {
            @Override
            public String format(int i) {
                return provinces[i];
            }
        });
        picker.setValue(selectProIndex);

        updatePicker2();
        //设置选中监听
        picker.setValueChangedListener(new Picker.ValueChangedListener() {
            @Override
            public void onValueChanged(Picker picker, int i, int i1) {
                selectProIndex = i1;
                updatePicker2();
            }
        });
    }

    private void updatePicker2() {
        picker2.setMinValue(0);
        picker2.setMaxValue(citys[selectProIndex].length-1);
        picker2.setFormatter(new Picker.Formatter() {
            @Override
            public String format(int i) {
                return citys[selectProIndex][i];
            }
        });

    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_pk_btn) {
            int value1 = picker.getValue();
            int value2 = picker2.getValue();
            String pro = provinces[value1];
            String city = citys[value1][value2];
            text.setText(pro+city);
        }
    }
}